package com.yenlien.traceyun.business.controller;

import com.yenlien.traceyun.business.entity.Product;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.ProductService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/7/1.
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody Product product) {
        product.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return buildPage(productService.findByList(product));
    }


    @RequestMapping(value = "/findOwnAll", method = RequestMethod.GET)
    public Object findOwnAll() {
        return productService.findAll(UserAuthUtil.getLoginUser().getCompanyId());
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(Product product, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        product.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        productService.save(product, files);
        return success("success");
    }

    @RequestMapping(value = "/save2", method = RequestMethod.POST)
    public Object save2(@RequestBody Product product) throws Exception {
        product.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        productService.save2(product);
        return success("success");
    }

//    @RequestMapping(value = "/save", method = RequestMethod.POST)
//    public Object save(Product product) throws Exception {
//        product.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
//        productService.save(product);
//        return success("success");
//    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(Product product, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        productService.update(product, files);
        return success("success");
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return success(productService.findById(id));
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Object delete(@PathVariable String id) {
        productService.delete(id);
        return success("success");
    }

    @RequestMapping(value = "/findByAll", method = RequestMethod.GET)
    public Object findByAll() {
        return success(productService.findAll(UserAuthUtil.getLoginUser().getCompanyId()));
    }

}
